package com.amazon.web.controller;

import com.amazon.bo.Shop;
import com.amazon.bo.ShopClass;
import com.amazon.bo.User;
import com.amazon.bo.enums.UserType;
import com.amazon.service.I18nService;
import com.amazon.service.ShopClassService;
import com.amazon.service.ShopService;
import com.amazon.web.AuthRequired;
import com.amazon.web.util.ApiListResponse;
import com.amazon.web.util.ApiResponse;
import com.amazon.web.util.OSSManageUtil;
import com.amazon.web.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping(value="shop")
public class ShopController {
    @Value("${is.open.cms}")
    public String isOpenCms;


    @Resource
    private OSSManageUtil ossManageUtil;

    @Resource
    ShopService shopService;

    @Autowired
    I18nService i18nService;

    @Resource
    ShopClassService shopClassService;

    @RequestMapping(value = "add", method = RequestMethod.POST)
    public String add(@RequestBody Shop shop) {
        Shop s=new Shop();
       Boolean b= shopService.saveShop(s);
        return "更新成功";

    }

    @AuthRequired
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public Object getShopList(@Value("#{request.getAttribute('currentUser')}") User currentUser){

        return shopService.selectShopList(currentUser.getId());
    }

    @AuthRequired
    @RequestMapping(value="info",method = RequestMethod.GET)
    public Object getShopInfo(@Value("#{request.getAttribute('currentUser')}") User currentUser){

        return shopService.selectShopInfo(currentUser.getId());
    }

    @RequestMapping(value="getshopList",method =RequestMethod.GET)
    public ApiResponse getshopList(int userid, int usertype, int page, int pagesize) {
         Object list=shopService.getShopList(userid,page,pagesize);
         return ResponseUtil.getSuccessResponse(list, 3);
    }

    @RequestMapping(value = "shopdetail", method = RequestMethod.GET)
    public Object getShopDetail(String shopid) {
        Object p=   shopService.getShopDetail(Integer.parseInt(shopid));
        List<ShopClass> scList=shopClassService.getClassList(Integer.parseInt(shopid));
        //p.setOperateClass(scList);
        Map<String,Object> result=new HashMap<>();
        if(p!=null) {
            result.put("shop", p);
            result.put("opClass", scList);
            result.put("flag", "success");
        }else{
            result.put("flag", "error");
        }
        return result;
    }
    @RequestMapping(value = "shopclassadd", method = RequestMethod.POST)
    public Object shopclassadd(ShopClass sc){
        Object o = shopClassService.save(sc);
        return o;
    }


    @RequestMapping(value = "getShopClassDetail", method = RequestMethod.GET)
    public ApiResponse getShopClassDetail(String shopClassid) {
        ShopClass sc=shopClassService.getById(Long.parseLong(shopClassid));
        return ResponseUtil.getSuccessResponse(sc);
    }

    @RequestMapping(value = "shopadd", method = RequestMethod.POST)
    public Boolean shopadd(Shop shop) {
        Boolean b= shopService.saveShop(shop);
        return b;
    }

}
